﻿using System;
using System.Collections.Generic;
using Castle.MonoRail.Framework;
using Castle.MonoRail.ViewComponents;
using StateTheater.Models;
using Castle.Core;
using System.Linq;

namespace StateTheater.Component
{
	public class ListProductsComponent : ViewComponentEx
	{
		[ViewComponentParam]
		public IEnumerable<Pair<string, string>> Products { get; set; }

		private const string m_referralBA = "njtheater0144";
		private const string m_referralSP = "1195";
		private const string m_referralAM = "njtheatercom-20";

		private string m_siteRoot;

		public override void Initialize()
		{
			base.Initialize();

		}

		public override void Render()
		{
			m_siteRoot = this.PropertyBag["siteRoot"] as string;
			base.Render();
			bool disclaim = false;

			Products = Products.OrderBy(pr => pr.First);

			foreach (Pair<string, string> product in Products)
			{
				switch (product.First)
				{
					case "BA":
//						RenderText(BroadwayArchive(product.Second));
						disclaim = true;
						break;

					case "SP":
						RenderText(StagePlays(product.Second));
						break;

					case "AV":
					case "AS":
					case "AC":
						RenderText(AmazonLink(product.Second));
						disclaim = true;
						break;
				}
			}
			if (disclaim)
			{
				RenderText(@"<font size=""-2""><BR />Note: These recording are not of the productions list here, but of Broadway or Hollywood versions.<br /></font>" + "\r\n");
			}
			this.CancelView();
		}

		private string ProductLink(string link, string text, string image)
		{
			const string format = @"<br/><a href=""{0}"" target=""_top"">" +
				@"<font size=""-2"">{1}</font><br/>" +
				@"<img src=""{2}"" /></a>" + "\r\n";

			return String.Format(format, link, text, image);
		}

		private string BroadwayArchive(string code)
		{
			string link = String.Format(@"http://www.broadwayarchive.com/catalog_detail.asp?id={0}&referral={1}", code, m_referralBA);
			string img = String.Format(@"{0}/content/images/broadwayarchive.gif", m_siteRoot);
			return ProductLink(link, "Buy the Video", img);
		}

		private string StagePlays(string code)
		{
			string link = String.Format(@"http://www.stageplays-partner.com/t.asp?id={0}&p=browse-no-frames.cgi?view=item&item={1}", m_referralSP, code);
			string img = @"http://www.stageplays.com/banners/button24.gif";
			return ProductLink(link, "Buy the Script", img);
		}

		private string AmazonLink(string asin)
		{
			string format = @"<iframe marginwidth=""0"" marginheight=""0"" width=""120"" height=""240"" scrolling=""no"" frameborder=""0"" src=""http://rcm.amazon.com/e/cm?o=1&l=as1&f=ifr&t=${0}&p=8&asins={1}&IS2=1&lt1=_blank"">"+
										@"<MAP NAME=""boxmap-p8"">"+
										@"<AREA SHAPE=""RECT"" COORDS=""14, 200, 103, 207"" HREF=""http://rcm.amazon.com/e/cm/privacy-policy.html?o=1"" />"+
										@"<AREA COORDS=""0,0,10000,10000"" HREF=""http://www.amazon.com/exec/obidos/redirect-home/${0}""/></MAP>"+
										@"<img src=""http://rcm-images.amazon.com/images/G/01/rcm/120x240.gif"" width=""120"" height=""240"" border=""0"" usemap=""#boxmap-p8"" alt=""Shop at Amazon.com""/>"+
										@"</iframe>"+"\r\n";
			return string.Format(format, m_referralAM, asin);
		}
	}
}
